﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BDGetQuote.Web.Models
{
    public class ManufacturerRepository : IManufacturerRepository
    {
        /// <summary>
        /// Get all manufacturers of user and system
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<Manufacturer> GetAll(string userid)
        {
            using (var _dbcontext =new BDQuoteDBDataContext())
            {
                var _result = _dbcontext.BO_Manufacturers.Where(x => !x.IsDeleted && (x.IsSystem || x.CreatedBy == userid) && !_dbcontext.BO_DeletedManufacturerByCustomers.Any(d=>d.ManufacturerId==x.ManufacturerId && d.UserId==userid))
                    .OrderBy(x => x.Name)
                    .Select(x => new Manufacturer()
                    {
                        ManufacturerId = x.ManufacturerId,
                        Name = x.Name,
                        CreatedBy = x.CreatedBy,
                        Description = x.Description,
                        ImageUrl = x.ImageUrl,
                        IsSystem = x.IsSystem
                    }).ToList();
                return _result;
            }
        }

        public IList<Manufacturer> GetAll(string userid,string keyword)
        {
            using (var _dbcontext = new BDQuoteDBDataContext())
            {
                var _result = _dbcontext.BO_Manufacturers.Where(x => !x.IsDeleted && (x.IsSystem || x.CreatedBy == userid) && ((x.Name + "").ToLower().Contains((keyword + "").ToLower()) || ((x.Description + "").ToLower().Contains((keyword + "").ToLower()))) && !_dbcontext.BO_DeletedManufacturerByCustomers.Any(d => d.ManufacturerId == x.ManufacturerId && d.UserId == userid))
                    .OrderBy(x => x.Name)
                    .Select(x => new Manufacturer()
                    {
                        ManufacturerId = x.ManufacturerId,
                        Name = x.Name,
                        CreatedBy = x.CreatedBy,
                        Description = x.Description,
                        ImageUrl = x.ImageUrl,
                        IsSystem = x.IsSystem
                    }).ToList();
                return _result;
            }
        }

        public void Edit(Manufacturer editItem)
        {
            using (var _dbcontext = new BDQuoteDBDataContext())
            {
                var _manufacturer = _dbcontext.BO_Manufacturers.FirstOrDefault(x => !x.IsDeleted && x.ManufacturerId == editItem.ManufacturerId);
                if (_manufacturer != null)
                {
                    _manufacturer.Name = editItem.Name;
                    _manufacturer.ImageUrl = editItem.ImageUrl;
                    _manufacturer.Description = editItem.Description;
                    _dbcontext.SubmitChanges();
                }
            }
        }

        public int Add(Manufacturer newItem)
        {
            using (var _dbcontext = new BDQuoteDBDataContext())
            {
                var _manufacturer = new BO_Manufacturer()
                {
                    Name = newItem.Name,
                    Description = newItem.Description,
                    IsSystem = newItem.IsSystem,
                    CreatedBy = newItem.CreatedBy,
                    ImageUrl = newItem.ImageUrl,
                    CreatedDate = DateTime.Now
                };
                _dbcontext.BO_Manufacturers.InsertOnSubmit(_manufacturer);
                _dbcontext.SubmitChanges();
                return _manufacturer.ManufacturerId;
            }
        }

        public void Delete(int id)
        {
            using (var _dbcontext = new BDQuoteDBDataContext())
            {
                var _manufacturer = _dbcontext.BO_Manufacturers.FirstOrDefault(x => x.ManufacturerId == id);
                if (_manufacturer !=null)
                {
                    try
                    {
                        _dbcontext.BO_Manufacturers.DeleteOnSubmit(_manufacturer);
                        _dbcontext.SubmitChanges();
                    }
                    catch
                    {
                        _manufacturer.IsDeleted = true;
                        _dbcontext.SubmitChanges();
                    }
                }
            }
        }

        public Manufacturer GetById(int id, string userid)
        {
            using (var _dbcontext = new BDQuoteDBDataContext())
            {
                var _user = _dbcontext.BO_UserInfos.FirstOrDefault(x => x.UserId == userid);
                if (_user == null)
                {
                    return null;
                }
                var _manufacturer = _dbcontext.BO_Manufacturers.FirstOrDefault(x => !x.IsDeleted && x.ManufacturerId == id && (_user.IsAdmin || x.CreatedBy == userid));
                if (_manufacturer !=null)
                {
                    return new Manufacturer()
                    {
                        ManufacturerId = _manufacturer.ManufacturerId,
                        Name = _manufacturer.Name,
                        Description = _manufacturer.Description,
                        CreatedBy = _manufacturer.CreatedBy,
                        CreatedDate = _manufacturer.CreatedDate,
                        ImageUrl = _manufacturer.ImageUrl,
                        IsSystem = _manufacturer.IsSystem
                    };
                }
                return null;
            }
        }
    }
}